package cn.aiyuan.controller;


import cn.aiyuan.config.RabbitmqConfig;
import cn.aiyuan.pojo.User;
import cn.aiyuan.service.ExcelTaskService;
import cn.aiyuan.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author aiyuan
 * @since 2020-09-29 09:54:22
 */
@Controller
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<User> page, User user) {
        return R.ok(this.userService.page(page, new QueryWrapper<>(user)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.userService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody User user) {
        return R.ok(this.userService.save(user));
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody User user) {
        return R.ok(this.userService.updateById(user));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.userService.removeByIds(idList));
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @PostMapping("/login")
    public String login(User user, Model model, HttpServletRequest httpServletRequest) {
        String username = user.getUsername();
        String password = user.getPassword();
        User one = userService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, username)
                .eq(User::getPassword, password));
        model.addAttribute("user",one);
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("userId",one.getId());
        model.addAttribute("session", session);
        //rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_FANOUT_INFORM,"",username);
        rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_FANOUT_INFORM,"sms",username);
        return "imIndex";
    }


    @Resource
    private ExcelTaskService excelTaskService;

    @GetMapping("/importExcel")
    @ResponseBody
    public String importExcel(HttpServletResponse response) throws InterruptedException {
        excelTaskService.threadExcel(response);
        return "ok";
    }

}
